package org.itworks.biodev.gui;

import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import javax.swing.JOptionPane;
import org.itworks.biodev.model.*;
import org.itworks.helpers.mvc.*;

public class CreateCustomerDialogControler extends AbstractControler {

	private CreateCustomerDialogModel model;
	
	public CreateCustomerDialogControler(CreateCustomerDialogModel model, Customer customer) {
		this.model = model;
		this.model.setCustomer(customer);
	}
	
	public CreateCustomerDialogModel getModel() {
		return this.model;
	}
	

	@Override
	public void viewLoaded() {
		// TODO Auto-generated method stub
		try {
			/*
			this.getModel().setCustomersList( CustomerTable.getInstance().findAllOrderedByLastName());
			
			if(this.model.getCustomer() != null) {
				//this.model.setIsUpdating(true);
				this.model.setInitCustomer(this.model.getCustomer());
			}
			*/
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	public void validateNewCustomerButtonActionPerformed(ActionEvent actionEvent, Customer customer, boolean edit) {
		// TODO Auto-generated method stub
		try {
			// Ajout du nouveau client
			//CustomerTable.getInstance().add(this.model.getCustomer());
			this.model.setCustomer(new Customer());
			this.model.getCustomer().setFirstName(customer.getFirstName());
			this.model.getCustomer().setLastName(customer.getLastName());
			this.model.getCustomer().setAddress(customer.getAddress());
			this.model.getCustomer().setCity(customer.getCity());
			this.model.getCustomer().setPostalCode(customer.getPostalCode());
			this.model.getCustomer().setPhoneNumber(customer.getPhoneNumber());
			this.model.getCustomer().setMail(customer.getMail());
			// Le client existe on le modifie
			if (customer.getId()!=null)
			{
				this.model.getCustomer().setId(customer.getId());
				CustomerTable.getInstance().update(customer);
			}
			// Il n'existe pas alors on le créé
			else
			{
				CustomerTable.getInstance().add(customer);
			}
			
			Vector<Customer> customers = CustomerTable.getInstance().findAll();
		} catch(Exception e) {
			e.printStackTrace();
		}	
	}
}
